package com.telsoft.util;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;

import com.telsoft.business.ComponentBusiness;
import com.telsoft.business.GroupComponentBusiness;
import com.telsoft.business.LibraryBusiness;
import com.telsoft.entity.ComponentEntity;
import com.telsoft.entity.GroupComponentEntity;
import com.telsoft.entity.LibraryEntity;
import com.telsoft.managedbean.function.Func_downloadleft;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

//@SuppressWarnings("serial")
@ManagedBean(name = "treeNode")
@SessionScoped
public class LeftProcess implements Serializable {

    private TreeNode root;
    private Func_downloadleft funcDownload;

    public LeftProcess() {
        root = new DefaultTreeNode(root, null);
        FacesContext context = FacesContext.getCurrentInstance();
        funcDownload = (Func_downloadleft) context.getApplication().evaluateExpressionGet(context, "#{func_downloadleft}", Func_downloadleft.class);

        try {
            List<LibraryEntity> librarys = LibraryBusiness.getLibrary();
            List<GroupComponentEntity> grComs = GroupComponentBusiness.getGRComponent();
            List<ComponentEntity> coms = ComponentBusiness.getComponent();
            for (LibraryEntity item : librarys) {
                TreeNode lib = new DefaultTreeNode(item.getName(), root);
                for (GroupComponentEntity itemGr : grComs) {
                    TreeNode grC = null;
                    if (itemGr.getLibID() == item.getLibID()) {
                        grC = new DefaultTreeNode(itemGr.getName(), lib);
                    }
                    for (ComponentEntity itemC : coms) {
                        if (itemC.getGrComID() == itemGr.getGrComID()) {
                            TreeNode c = new DefaultTreeNode(itemC.getName(), grC);
                        }
                    }
                }
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public TreeNode getRoot() {
        return root;
    }

    public void setRoot(TreeNode root) {
        this.root = root;
    }

    public Func_downloadleft getFuncDownload() {
        return funcDownload;
    }

    public void setFuncDownload(Func_downloadleft funcDownload) {
        this.funcDownload = funcDownload;
    }

    
}
